import request from '@/utils/request'
import { getToken, removeToken, setToken } from '@/utils/auth'
import { logout } from '@/api/login'
import { interfaceLogin } from '@/api/deal/indicator/data/dealIndicatorDataDetail'

// 查询数据填写维护邮箱列表
export function listDealIndicatorEmail(query) {
  return request({
    url: '/indicator/dealIndicatorEmail/list',
    method: 'get',
    params: query
  })
}

// 查询数据填写维护邮箱详细
export function getDealIndicatorEmail(id) {
  return request({
    url: '/indicator/dealIndicatorEmail/' + id,
    method: 'get'
  })
}

// 保存数据填写维护邮箱
export function saveDealIndicatorEmail(data) {
  return request({
    url: '/indicator/dealIndicatorEmail/save',
    method: 'post',
    data: data
  })
}

// 修改数据填写维护邮箱
export function updateDealIndicatorEmail(data) {
  return request({
    url: '/indicator/dealIndicatorEmail/updateData',
    method: 'post',
    data: data
  })
}

// 删除数据填写维护邮箱
export function delDealIndicatorEmail(id) {
  return request({
    url: '/indicator/dealIndicatorEmail/' + id,
    method: 'delete'
  })
}

// 导出数据填写维护邮箱
export function exportDealIndicatorEmail(query) {
  return request({
    url: '/indicator/dealIndicatorEmail/export',
    method: 'get',
    params: query
  })
}

const indicatorUser = {
  state: {
    email: '',
    token: getToken(),
    userId: '',
    name: '',
    loginName: '',
    avatar: '',
    roles: [],
    permissions: [],
    userTree: [],
    deptOption: []
  },

  mutations: {
    SET_EMAIL: (state, email) => {
      state.email = email
    },
    SET_TOKENS: (state, token) => {
      state.token = token
    },
    SET_USERID: (state, userId) => {
      state.userId = userId
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_LOGINNAME: (state, name) => {
      state.loginName = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_USER_LIST: (state, userTree) => {
      state.userList = userTree
    },
    SET_USER_TREE: (state, userTree) => {
      state.userTree = userTree
    },
    SET_DEPT: (state, options) => {
      state.deptOption = options
    }
  },

  actions: {
    // 登录
    IndicatorLogin({ commit }, userInfo) {
      const dataId = userInfo.dataId
      const username = userInfo.email.trim()
      const password = userInfo.emailCode
      // 多租户编号
      const tenantCode = userInfo.tenantCode
      return new Promise((resolve, reject) => {
        interfaceLogin({
          dataId: dataId,
          email: username,
          emailCode: password,
          tenantCode: tenantCode
        }).then(res => {
          setToken(res.datas.temp_token)
          commit('SET_TOKENS', res.datas.temp_token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 退出系统
    IndicatorLogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKENS', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default indicatorUser
